home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1993 December
/
PSL Monthly Shareware CD-ROM (December 1993).iso
/
prgmming
/
dos
/
pascal
/
p_mat.exe
/
PMAT.DOC
< prev
next >
Wrap
Text File
|
1993-01-24
|
49KB
|
1,311 lines
P-Mat v1.2
An Turbo Pascal program for Recursive Matrix Algebra
Mark Von Tress, Ph.D.
PO Box 171173
Arlington TX 76003
Compuserve User ID : 71530,1170
Date: January 30, 1993
You are responsible for what you do with the
code. Here is a formal disclaimer:
DISCLAIMER: THIS PROGRAM IS PROVIDED AS IS, WITHOUT ANY
WARRANTY, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
TO FITNESS FOR A PARTICULAR PURPOSE. THE AUTHOR DISCLAIMS
ALL LIABILITY FOR DIRECT OR CONSEQUENTIAL DAMAGES RESULTING
FROM USE OF THIS PROGRAM.
Copyright (c) Mark Von Tress 1993
Mat-P 3
Table of Contents
I. INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . 4
II. FINANCIAL CONDITIONS OF USE . . . . . . . . . . . . . . . 4
III. BASIC ALGORITHMS . . . . . . . . . . . . . . . . . . . . 5
A. Structures and Allocation . . . . . . . . . . . . . . 5
B. The global stack: dispatch . . . . . . . . . . . . . 7
C. Recursion . . . . . . . . . . . . . . . . . . . . . . 7
D. Matrix Assignment . . . . . . . . . . . . . . . . . . 8
E. Parameter Passing . . . . . . . . . . . . . . . . . . 9
IV. FUNCTIONS . . . . . . . . . . . . . . . . . . . . . . . . 10
A. Error Handling . . . . . . . . . . . . . . . . . . . 10
B. Array Allocation or Deallocation . . . . . . . . . . 11
C. Stack Control . . . . . . . . . . . . . . . . . . . . 11
D. Matrix Allocation, Deallocation and Copy . . . . . . 13
E. Display Functions . . . . . . . . . . . . . . . . . . 14
F. Matrix IO . . . . . . . . . . . . . . . . . . . . . . 15
G. Binary Matrix Functions . . . . . . . . . . . . . . . 16
H. Unary Matrix Functions . . . . . . . . . . . . . . . 17
I. Patterned Matrices . . . . . . . . . . . . . . . . . 19
J. Mathematical Functions . . . . . . . . . . . . . . . 20
V. COMPILATION AND LIMITATIONS . . . . . . . . . . . . . . . 24
VI. REVISION HISTORY . . . . . . . . . . . . . . . . . . . . 25
A. Version 1.0 . . . . . . . . . . . . . . . . . . . . 25
B. Version 1.1 . . . . . . . . . . . . . . . . . . . . 25
C. Version 1.2 . . . . . . . . . . . . . . . . . . . . 25
Mat-P 4
I. INTRODUCTION
PMAT is Turbo Pascal (TP) source code for recursive matrix
algebra. The program is based on another program I wrote in C to
do the same thing. Both use a stack of matrices to keep track of
intermediate heap allocations. Once I figured it out in C it was
pretty easy to step back and do it in Pascal. The object
oriented extensions in TP also helped smooth the process. This
allowed a convenient scheme to allow matrices to be larger than
64K.
Of course you can't overload operators in TP, so the recursion is
messy. However, you can keep track of intermediate allocations on
the heap using PMAT.
new( a, makematrix( 1, 1 ) );
x = matequals(x, add( a,add(b,c)));
This code segment has to keep track of matrix allocations on the
heap, and then delete the temporary matrices. In this example,
the sum of B and C is a temporary matrix which would be lost
without some sort of global memory allocation tracking such as a
stack. Its memory should be deleted after the equals function is
called. The stack helps avoid the assignment of temporaries in
recursive calls.
On a more personal note, I wrote this program for fun. I started
on this problem in TP 3.0 when that was the best compiler on the
market (1984). I never really got a satisfactory answer, so I set
it down. Then I picked it up in Turbo C, and didn't get a
satisfactory answer. Then I picked it up in C++, and got a good
answer in about 1990 (see YAMP on the BPROGB of Compuserve). Then
I wrote it in C with good results, then TP 6.0 just for old times
sake. I had forgotten how fast TP compiles. It's a real code
blaster. Object oriented programming in TP 6 also helped. I guess
I learned some computer science along the way too.
II. FINANCIAL CONDITIONS OF USE
If you like this program, send me $5.00 US (or buy a deserving
beggar lunch. In either case, find a way to repay my charity.)
You may distribute the package unchanged. I only plan to
distribute it electronically. I retain the copyright as proof of
authorship.
Mat-P 5
III. BASIC ALGORITHMS
A. Structures and Allocation
PMAT has two important objects: vmatrix and mstack. The vmatrix
object is declared in the interface
vmatrixptr = ^vmatrix;
vmatrix = Object
r, c : integer;
Function m( i, j: integer ): double;
Function mm( i, j: integer ) : fp;
constructor MakeMatrix( vr, vc: integer );
Destructor KillVmatrix;
Procedure Garbage;
Procedure Show( strng: String );
Procedure infomatrix( strng: String );
private
v,vcheck: ^app;
nelems : longint;
onstack : boolean;
Procedure purgevectors;
Procedure allocvectors( rr, cc: integer );
End;
The vmatrix contains integers for the dimensions of the matrices,
an array of pointers to vectors of doubles, the number of
elements, and a check address. Matrix allocation is based on
Numerical Recipes in C . An array of addresses of vectors of
doubles is allocated first. Then, an array is allocated and its
first address is stored in the address vector. This method allows
matrices larger than 64K and scatters the rows of the matrix
throughout the heap. A row is restricted to 8190 doubles to keep
it under 64K.
Object oriented programming helped hide the ugly notation
required for storing and retrieving elements from a vmatrix. You
use the member function mm(i,j) to store elements in the matrix,
and m(i,j) to retrieve elements. An example is
x^.mm(i,j)^ := y^.m(i,j);
where x and y are vmatrix pointers. Note the extra ^ in
x^.mm(i,j)^. This returns the address in the heap for storing an
element. Using x^.mm(i,j) won't work. Think of element assignment
as saying "put y^.m(i,j) in the address pointed to by
x^.mm(i,j)". Using m(i,j) returns a double via
Mat-P 6
m := v^[i]^[j];
and using mm(i,j) returns an address via
mm := @v^[i]^[j];
Using member functions protects you from having to type these
unintuitive indexing operations. I also allows easy access to
matrices larger than 64K. They also do range checking on the
indexes.
The check pointer contains the value of v upon allocating the
matrix. Then it is set to zero upon deallocation of v. The check
value is used in the member function Garbage(). If v does not
equal vcheck, then t